package com.ferry.wallpaper.interceptor;

import com.ferry.wallpaper.utils.RandomArtUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        if (session.getAttribute("user") == null) {
            session.setAttribute("say",false);
            session.setAttribute("add",true);
            request.setAttribute("msg","你还没登陆，请先点击右上角登录！");
            request.setAttribute("sayer", RandomArtUtils.getRandom());
            request.getRequestDispatcher(request.getContextPath() + "/").forward(request,response);
            return false;
        } else {
            session.setAttribute("say",true);
            session.setAttribute("add",false);
            return true;
        }
    }
}
